#import "DirectionsGame.h"

@interface DirectionsGame ()

@property NSArray* colors;
@property BOOL isGameOver;
@property NSArray* directions;
@property NSInteger currentTrans;
@property NSInteger currentDirection;

@end

@implementation DirectionsGame

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"DirectionsGame"];
        [self setType:reaction];
        
        self.directions=[[NSArray alloc]initWithObjects:@"Left",@"Up",@"Right",@"Down", nil];

        
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        
    }
    return self;
}

-(void)draw{
    [self removeAllComponents];
    
    int firstNumber=arc4random_uniform(4);
    self.currentTrans=arc4random_uniform(4);
    self.currentDirection=firstNumber;
    
    
    int colour=arc4random_uniform(7);
    
    
    CGFloat trans=1.57 * self.currentTrans;
    
    
    NSString* labelText=[NSString stringWithFormat:@"%@",[self.directions objectAtIndex:firstNumber]];
    NSString* labelColor=[NSString stringWithFormat:@"%@Color",[self.colors objectAtIndex:colour]];
    
    if (firstNumber < 2) {
        firstNumber=firstNumber+2;
    }
    else{
        firstNumber=firstNumber-2;
    }
    
    NSString* labelOneText=[NSString stringWithFormat:@"%@",[self.directions objectAtIndex:firstNumber]];
    
    
    LabelGameComponent* label=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:labelColor];
    LabelGameComponent* labelOne=[[LabelGameComponent alloc]initWithText:labelOneText andFrame:CGRectMake(0, kScreenHeight-kScreenHeight/kHeighDel - 2*kButtonHeigh, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:labelColor];
    
    [self addComponents:label];
    [self addComponents:labelOne];
    
    
    ImageGameComponent* image=[[ImageGameComponent alloc]initWithName:@"direction.png" andFrame:CGRectMake(kScreenWidth/2.8, kScreenHeight/6, kScreenHeight/6, kScreenHeight/6)andTrans:trans];
    [self addComponents:image];

    
}

-(BOOL)isRight{
    if (self.currentDirection==self.currentTrans) {
        return YES;
        
    }
    else{
        return NO;
    }
}

-(void)play{
    [self draw];
    
    NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval: 2                                                 target: self selector:@selector(newDirection:)userInfo: nil repeats:YES];
    
}

-(void)newDirection:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self draw];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames" object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}

@end
